<?php
if (!defined('BASEPATH')) {
	exit('No direct script access allowed');
}

class m_transaksi_penjualan extends CI_Model {

	public function __construct() {
		parent::__construct();
	}

	function get() {
		$sql = 'SELECT * FROM jual 
		INNER JOIN rc_users ON rc_users.id = jual.id_user ORDER by KdJual DESC';
		return $sql;
	}

	function get_cari($tgl_awal,$tgl_akhir,$petugas,$kelompok,$obat) {
		if($petugas != ''){
			$where_petugas = "AND rc_users.id = '".$petugas."'";
		}else{
			$where_petugas = "";
		}

		if($kelompok != ''){
			$where_kelompok = "AND obat.KdKelompok = '".$kelompok."'";
		}else{
			$where_kelompok = "";
		}

		if($obat != ''){
			$where_obat = "AND detail_jual.KdObat = '".$obat."'";
		}else{
			$where_obat = "";
		}

		$sql = 'SELECT nickname, jual.KdJual, Tanggal, Keterangan, id_user FROM jual 
		INNER JOIN rc_users ON rc_users.id = jual.id_user
		INNER JOIN detail_jual ON jual.KdJual = detail_jual.KdJual
		INNER JOIN obat ON detail_jual.KdObat = obat.KdObat
		WHERE tanggal between "'.$tgl_awal.'" AND "'.$tgl_akhir.'" 
		'.$where_petugas.'
		'.$where_kelompok.'
		'.$where_obat.'
		GROUP by KdJual
		ORDER by KdJual DESC';
		return $sql;
	}

	function get_cari_non_tgl($petugas,$kelompok,$obat) {
		if($petugas != ''){
			$where_petugas = "AND rc_users.id = '".$petugas."'";
		}else{
			$where_petugas = "";
		}

		if($kelompok != ''){
			$where_kelompok = "AND obat.KdKelompok = '".$kelompok."'";
		}else{
			$where_kelompok = "";
		}

		if($obat != ''){
			$where_obat = "AND detail_jual.KdObat = '".$obat."'";
		}else{
			$where_obat = "";
		}

		$sql = 'SELECT nickname, jual.KdJual, Tanggal, Keterangan, id_user FROM jual 
		INNER JOIN rc_users ON rc_users.id = jual.id_user
		INNER JOIN detail_jual ON jual.KdJual = detail_jual.KdJual
		INNER JOIN obat ON detail_jual.KdObat = obat.KdObat
		WHERE jual.KdJual <> ""
		'.$where_petugas.'
		'.$where_kelompok.'
		'.$where_obat.'
		GROUP by KdJual
		ORDER by KdJual DESC';
		return $sql;
	}

	function get_obat() {
		$sql = 'SELECT * FROM obat';
		$query = $this->db->query($sql);
		if ($query->num_rows() > 0) {
			$result = $query->result_array();
			$query->free_result();
		} else {
			$result = array();
		}
		return $result;
	}

	function get_kelompok() {
		$sql = 'SELECT * FROM kelompok';
		$query = $this->db->query($sql);
		if ($query->num_rows() > 0) {
			$result = $query->result_array();
			$query->free_result();
		} else {
			$result = array();
		}
		return $result;
	}

	function get_petugas() {
		$sql = 'SELECT rc_users.id, nickname FROM rc_users
			INNER JOIN rc_users_groups ON rc_users.id = rc_users_groups.user_id
			INNER JOIN rc_groups ON rc_users_groups.group_id = rc_groups.id
			WHERE rc_groups.name = "admin" OR rc_groups.name = "kasir"
			GROUP BY rc_users.id';
		$query = $this->db->query($sql);
		if ($query->num_rows() > 0) {
			$result = $query->result_array();
			$query->free_result();
		} else {
			$result = array();
		}
		return $result;
	}

	function get_obat_by($KdObat) {
		$sql = 'SELECT * FROM obat WHERE KdObat = ?';
		echo $sql;
		$query = $this->db->query($sql,$KdObat);
		if ($query->num_rows() > 0) {
			$result = $query->row_array();
			$query->free_result();
		} else {
			$result = array();
		}
		return $result;
	}

	public function get_detail_by_id($id){
		$this->db->where('KdJual', $id);
		$this->db->join('obat','obat.KdObat = detail_jual.KdObat');
		$query = $this->db->get('detail_jual')->result_array();
		return $query;
	}

	public function get_by_id($id){
		$this->db->where('KdJual', $id);
		$this->db->join('rc_users', 'jual.id_user = rc_users.id');
		$query = $this->db->get('jual');

		if($query->num_rows() > 0){
			$result = $query->row_array();
			$query->free_result();
		}else{
			$result = array();
		}

		return $result;
	}

	function get_export_all() {
		$sql = 'SELECT * FROM jual 
		INNER JOIN rc_users ON rc_users.id = jual.id_user ORDER by KdJual DESC';
		$query = $this->db->query($sql);
		if ($query->num_rows() > 0) {
			$result = $query->result_array();
			$query->free_result();
		} else {
			$result = array();
		}
		return $result;
	}

	function get_export($tgl_awal,$tgl_akhir,$petugas,$kelompok,$obat) {
		if($petugas != ''){
			$where_petugas = "AND rc_users.id = '".$petugas."'";
		}else{
			$where_petugas = "";
		}

		if($kelompok != ''){
			$where_kelompok = "AND obat.KdKelompok = '".$kelompok."'";
		}else{
			$where_kelompok = "";
		}

		if($obat != ''){
			$where_obat = "AND detail_jual.KdObat = '".$obat."'";
		}else{
			$where_obat = "";
		}

		$sql = 'SELECT nickname, jual.KdJual, Tanggal, Keterangan, id_user FROM jual 
		INNER JOIN rc_users ON rc_users.id = jual.id_user
		INNER JOIN detail_jual ON jual.KdJual = detail_jual.KdJual
		INNER JOIN obat ON detail_jual.KdObat = obat.KdObat
		WHERE tanggal between "'.$tgl_awal.'" AND "'.$tgl_akhir.'" 
		'.$where_petugas.'
		'.$where_kelompok.'
		'.$where_obat.'
		GROUP by KdJual
		ORDER by KdJual DESC';
		$query = $this->db->query($sql);
		if ($query->num_rows() > 0) {
			$result = $query->result_array();
			$query->free_result();
		} else {
			$result = array();
		}
		return $result;
	}

	function get_export_non_tgl($petugas,$kelompok,$obat) {
		if($petugas != ''){
			$where_petugas = "AND rc_users.id = '".$petugas."'";
		}else{
			$where_petugas = "";
		}

		if($kelompok != ''){
			$where_kelompok = "AND obat.KdKelompok = '".$kelompok."'";
		}else{
			$where_kelompok = "";
		}

		if($obat != ''){
			$where_obat = "AND detail_jual.KdObat = '".$obat."'";
		}else{
			$where_obat = "";
		}

		$sql = 'SELECT nickname, jual.KdJual, Tanggal, Keterangan, id_user FROM jual 
		INNER JOIN rc_users ON rc_users.id = jual.id_user
		INNER JOIN detail_jual ON jual.KdJual = detail_jual.KdJual
		INNER JOIN obat ON detail_jual.KdObat = obat.KdObat
		WHERE jual.KdJual <> ""
		'.$where_petugas.'
		'.$where_kelompok.'
		'.$where_obat.'
		GROUP by KdJual
		ORDER by KdJual DESC';
		$query = $this->db->query($sql);
		if ($query->num_rows() > 0) {
			$result = $query->result_array();
			$query->free_result();
		} else {
			$result = array();
		}
		return $result;
	}

	function saveTransaksi($dataInsert){
		$query = $this->db->insert('jual', $dataInsert);
		return $query;
	}

	function saveDetailTransaksi($dataDetailInsert){
		$query = $this->db->insert('detail_jual', $dataDetailInsert);
		return $query;
	}
}
